package 笔试强训;

import java.util.LinkedList;
import java.util.Scanner;

public class _0416T3 {
    public static void main(String[] args) {
        String[] strs = new String[10000000];
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        scanner.nextLine();
        String ss = scanner.nextLine();
        String[] str = ss.split(" ");
        if (str[0] == null || str[1] == null) {
            System.out.println(-1);
        }
        int index1 = -1;
        int index2 = -1;
        int i = 0;
        while (i != n) {
            boolean flag = false;
            strs[i] = scanner.nextLine();
            if (str[0].equals(strs[i])) {
                if (index1 == -1) {
                    index1 = i;
                } else {
                    if (Math.abs(index2 - i) < Math.abs(index2 - index1)) {
                        index1 = i;
                    }
                }
            }
            if (str[1].equals(strs[i])) {
                if (index2 == -1) {
                    index2 = i;
                } else {
                    if (Math.abs(index1 - i) < Math.abs(index2 - index1)) {
                        index2 = i;
                    }
                  }
            }
            n--;
        }
        if (index1 == -1 || index2 == -1) {
            System.out.println(-1);
        } else {
            System.out.println(Math.abs(index2 - index1+1));
        }
    }
}

